@echo off
chcp 65001
setlocal enabledelayedexpansion

REM 物业保洁系统Windows一键部署脚本
REM 支持开发、测试、生产环境部署

title 物业保洁系统部署工具

set "ENVIRONMENT=%~1"
set "BACKUP=false"
set "SKIP_TESTS=false"
set "LOG_FILE=%TEMP%\deploy-%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log"

REM 颜色定义
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "NC=[0m"

REM 帮助信息
:show_help
echo.
echo 物业保洁系统部署工具
echo ====================
echo.
echo 用法: %~nx0 [环境] [选项]
echo.
echo 环境:
echo   dev     开发环境部署
echo   test    测试环境部署
echo   prod    生产环境部署
echo.
echo 选项:
echo   --help     显示帮助信息
echo   --backup   部署前创建备份
echo   --stop     停止服务
echo   --status   显示状态
echo   --health   健康检查
echo.
echo 示例:
echo   %~nx0 dev
echo   %~nx0 prod --backup
echo.
goto :eof

REM 日志函数
:log
echo %GREEN%[%date% %time%]%NC% %~1
echo [%date% %time%] %~1 >> "%LOG_FILE%"
goto :eof

:error
echo %RED%[%date% %time%] ERROR:%NC% %~1
echo [%date% %time%] ERROR: %~1 >> "%LOG_FILE%"
goto :eof

:warning
echo %YELLOW%[%date% %time%] WARNING:%NC% %~1
echo [%date% %time%] WARNING: %~1 >> "%LOG_FILE%"
goto :eof

REM 检查环境
:check_environment
call :log "检查环境: %ENVIRONMENT%"

REM 检查Docker
where docker >nul 2>nul
if %errorlevel% neq 0 (
    call :error "Docker未安装"
    pause
    exit /b 1
)

REM 检查Docker Compose
where docker-compose >nul 2>nul
if %errorlevel% neq 0 (
    call :error "Docker Compose未安装"
    pause
    exit /b 1
)

REM 检查必要目录
if not exist "backend" (
    call :error "缺少backend目录"
    pause
    exit /b 1
)

if not exist "frontend" (
    call :error "缺少frontend目录"
    pause
    exit /b 1
)

goto :eof

REM 开发环境部署
:deploy_dev
call :log "开始部署开发环境..."

REM 启动MySQL
call :log "启动MySQL..."
docker ps | findstr "property-mysql-dev" >nul
if %errorlevel% neq 0 (
    docker run -d --name property-mysql-dev ^
        -e MYSQL_ROOT_PASSWORD=root123 ^
        -e MYSQL_DATABASE=property_cleaning ^
        -e MYSQL_USER=dev_user ^
        -e MYSQL_PASSWORD=dev_pass ^
        -p 3306:3306 ^
        -v mysql_dev_data:/var/lib/mysql ^
        mysql:8.0
) else (
    call :warning "MySQL容器已存在"
)

REM 启动Redis
call :log "启动Redis..."
docker ps | findstr "property-redis-dev" >nul
if %errorlevel% neq 0 (
    docker run -d --name property-redis-dev ^
        -p 6379:6379 ^
        -v redis_dev_data:/data ^
        redis:7-alpine
) else (
    call :warning "Redis容器已存在"
)

REM 等待数据库启动
call :log "等待数据库启动..."
timeout /t 10 /nobreak >nul

REM 初始化数据库
if exist "database\schema.sql" (
    call :log "初始化数据库..."
    docker exec -i property-mysql-dev mysql -u root -proot123 property_cleaning < database\schema.sql
)

REM 启动后端
call :log "构建并启动后端..."
cd backend
if exist "mvnw.cmd" (
    call mvnw.cmd clean package -DskipTests
) else (
    call mvn clean package -DskipTests
)

start "" cmd /c "java -jar -Dspring.profiles.active=dev target\property-cleaning-backend-1.0.0.jar"
cd ..

REM 启动前端
call :log "构建并启动前端..."
cd frontend
call npm install
call npm run dev
cd ..

call :log "开发环境部署完成！"
call :log "前端: http://localhost:3000"
call :log "后端: http://localhost:8080"
call :log "MySQL: localhost:3306"
call :log "Redis: localhost:6379"

goto :eof

REM 测试环境部署
:deploy_test
call :log "开始部署测试环境..."

if exist "docker-compose.test.yml" (
    call :log "使用现有测试配置..."
    docker-compose -f docker-compose.test.yml down
    docker-compose -f docker-compose.test.yml up -d --build
) else (
    call :log "创建测试配置..."
    (
        echo version: '3.8'
        echo services:
        echo   mysql-test:
        echo     image: mysql:8.0
        echo     container_name: property-mysql-test
        echo     environment:
        echo       MYSQL_ROOT_PASSWORD: test_root_2024
        echo       MYSQL_DATABASE: property_cleaning_test
        echo       MYSQL_USER: test_user
        echo       MYSQL_PASSWORD: test_pass_2024
        echo     ports:
        echo       - "3307:3306"
        echo     volumes:
        echo       - mysql_test_data:/var/lib/mysql
        echo       - ./database/schema.sql:/docker-entrypoint-initdb.d/schema.sql
        echo.
        echo   redis-test:
        echo     image: redis:7-alpine
        echo     container_name: property-redis-test
        echo     ports:
        echo       - "6380:6379"
        echo.
        echo   backend-test:
        echo     build: ./backend
        echo     container_name: property-backend-test
        echo     depends_on:
        echo       - mysql-test
        echo       - redis-test
        echo     environment:
        echo       SPRING_PROFILES_ACTIVE: test
        echo       SPRING_DATASOURCE_URL: jdbc:mysql://mysql-test:3306/property_cleaning_test
        echo       SPRING_DATASOURCE_USERNAME: test_user
        echo       SPRING_DATASOURCE_PASSWORD: test_pass_2024
        echo     ports:
        echo       - "8081:8080"
        echo.
        echo   frontend-test:
        echo     build: ./frontend
        echo     container_name: property-frontend-test
        echo     depends_on:
        echo       - backend-test
        echo     ports:
        echo       - "3001:80"
        echo     environment:
        echo       - VITE_API_BASE_URL=http://localhost:8081
        echo.
        echo volumes:
        echo   mysql_test_data:
    ) > docker-compose.test.yml
    
    docker-compose -f docker-compose.test.yml up -d --build
)

call :log "测试环境部署完成！"
call :log "前端: http://localhost:3001"
call :log "后端: http://localhost:8081"
call :log "MySQL: localhost:3307"
call :log "Redis: localhost:6380"

goto :eof

REM 生产环境部署
:deploy_prod
call :log "开始部署生产环境..."

echo %YELLOW%警告: 即将部署到生产环境%NC%
set /p confirm=确认部署到生产环境? (y/N): 
if /i not "%confirm%"=="y" (
    call :log "取消生产环境部署"
    goto :eof
)

if exist "docker-compose.prod.yml" (
    call :log "使用生产配置部署..."
    docker-compose -f docker-compose.prod.yml down
    docker-compose -f docker-compose.prod.yml up -d --build
) else (
    call :log "创建生产配置..."
    (
        echo version: '3.8'
        echo services:
        echo   mysql-prod:
        echo     image: mysql:8.0
        echo     container_name: property-mysql-prod
        echo     environment:
        echo       MYSQL_ROOT_PASSWORD: prod_root_secure_2024
        echo       MYSQL_DATABASE: property_cleaning_prod
        echo       MYSQL_USER: prod_user
        echo       MYSQL_PASSWORD: prod_pass_secure_2024
        echo     ports:
        echo       - "3306:3306"
        echo     volumes:
        echo       - mysql_prod_data:/var/lib/mysql
        echo       - ./database/schema.sql:/docker-entrypoint-initdb.d/schema.sql
        echo     command: --default-authentication-plugin=mysql_native_password
        echo.
        echo   redis-prod:
        echo     image: redis:7-alpine
        echo     container_name: property-redis-prod
        echo     ports:
        echo       - "6379:6379"
        echo     volumes:
        echo       - redis_prod_data:/data
        echo     command: redis-server --requirepass prod_redis_2024
        echo.
        echo   backend-prod:
        echo     build: ./backend
        echo     container_name: property-backend-prod
        echo     depends_on:
        echo       - mysql-prod
        echo       - redis-prod
        echo     environment:
        echo       SPRING_PROFILES_ACTIVE: prod
        echo       SPRING_DATASOURCE_URL: jdbc:mysql://mysql-prod:3306/property_cleaning_prod
        echo       SPRING_DATASOURCE_USERNAME: prod_user
        echo       SPRING_DATASOURCE_PASSWORD: prod_pass_secure_2024
        echo     ports:
        echo       - "8080:8080"
        echo     restart: unless-stopped
        echo.
        echo   frontend-prod:
        echo     build: ./frontend
        echo     container_name: property-frontend-prod
        echo     depends_on:
        echo       - backend-prod
        echo     ports:
        echo       - "80:80"
        echo     restart: unless-stopped
        echo     environment:
        echo       - VITE_API_BASE_URL=http://localhost:8080
        echo.
        echo volumes:
        echo   mysql_prod_data:
        echo   redis_prod_data:
    ) > docker-compose.prod.yml
    
    docker-compose -f docker-compose.prod.yml up -d --build
)

call :log "生产环境部署完成！"
call :log "前端: http://localhost"
call :log "后端: http://localhost:8080"
call :log "MySQL: localhost:3306"
call :log "Redis: localhost:6379"

goto :eof

REM 健康检查
:health_check
call :log "执行健康检查..."

REM 检查MySQL
docker ps | findstr "property-mysql" >nul
if %errorlevel%==0 (
    call :log "√ MySQL服务运行正常"
) else (
    call :error "MySQL服务未运行"
)

REM 检查Redis
docker ps | findstr "property-redis" >nul
if %errorlevel%==0 (
    call :log "√ Redis服务运行正常"
) else (
    call :error "Redis服务未运行"
)

REM 检查后端
curl -s http://localhost:8080/actuator/health >nul 2>nul
if %errorlevel%==0 (
    call :log "√ 后端服务运行正常"
) else (
    call :error "后端服务未运行"
)

REM 检查前端
curl -s http://localhost:3000 >nul 2>nul
if %errorlevel%==0 (
    call :log "√ 前端服务运行正常"
) else (
    call :error "前端服务未运行"
)

goto :eof

REM 停止服务
:stop_services
call :log "停止服务..."

REM 停止开发环境容器
docker stop property-mysql-dev property-redis-dev 2>nul
docker rm property-mysql-dev property-redis-dev 2>nul

REM 停止测试环境
docker-compose -f docker-compose.test.yml down 2>nul

REM 停止生产环境
docker-compose -f docker-compose.prod.yml down 2>nul

call :log "服务已停止"
goto :eof

REM 显示状态
:show_status
call :log "当前服务状态:"
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
goto :eof

REM 主函数
:main
if "%ENVIRONMENT%"=="--help" goto :show_help
if "%ENVIRONMENT%"=="--stop" goto :stop_services
if "%ENVIRONMENT%"=="--status" goto :show_status
if "%ENVIRONMENT%"=="--health" goto :health_check

if "%ENVIRONMENT%"=="" set "ENVIRONMENT=dev"

call :log "开始部署到 %ENVIRONMENT% 环境"

call :check_environment

if "%ENVIRONMENT%"=="dev" goto :deploy_dev
if "%ENVIRONMENT%"=="test" goto :deploy_test
if "%ENVIRONMENT%"=="prod" goto :deploy_prod

call :error "未知环境: %ENVIRONMENT%"
goto :show_help

pause